package com.hx.zhg.web.config;


import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

@Configuration
@PropertySource("classpath:properties/zk.properties")
public class ElasticJobConfig implements EnvironmentAware {

    private Environment env;

    @Override
    public void setEnvironment(Environment environment) {
        env = environment;
    }

    /**
     * zookeeper config
     *
     * @return
     */
    @Bean
    public ZookeeperConfiguration zkConfig() {
        ZookeeperConfiguration config = new ZookeeperConfiguration(
                env.getProperty("elasticjob.zookeeper.list"),
                env.getProperty("elasticjob.zookeeper.namespace"));
        config.setBaseSleepTimeMilliseconds(1000);
        config.setMaxRetries(3);
        config.setMaxSleepTimeMilliseconds(3000);
        config.setConnectionTimeoutMilliseconds(3000);
        return config;
    }

    /**
     * 初始化注册
     *
     * @param config
     * @return
     */
    @Bean(initMethod = "init")
    public ZookeeperRegistryCenter regCenter(ZookeeperConfiguration config) {
        ZookeeperRegistryCenter zookeeperRegistryCenter = new ZookeeperRegistryCenter(config);
        return zookeeperRegistryCenter;
    }

}
